package com.serkol.driving.learning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.bean.MyPage;
import com.serkol.driving.learning.entity.CategorySetting;
import com.serkol.driving.learning.entity.QuestionBank;
import com.serkol.driving.learning.handler.QuestionMapResult;
import com.serkol.driving.learning.mapper.QuestionBankMapper;
import com.serkol.driving.learning.model.SqlModel;
import com.serkol.driving.learning.service.CategorySettingService;
import com.serkol.driving.learning.service.QuestionBankService;
import com.serkol.driving.learning.handler.MyLambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author pingan
 * @description 针对表【question_bank】的数据库操作Service实现
 * @createDate 2022-06-07 00:16:41
 */
@Service
@CacheConfig(cacheNames = "question")
public class QuestionBankServiceImpl extends ServiceImpl<QuestionBankMapper, QuestionBank>
        implements QuestionBankService {
    @Autowired
    private CategorySettingService categorySettingService;

    @Override
    public MyPage findByList(int pageSize, int current) {
        QueryWrapper<QuestionBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("SORT");
        Page<QuestionBank> page = new Page<>(current, pageSize);
        IPage<QuestionBank> loginLogPage = this.baseMapper.selectPage(page, queryWrapper);
        return MyPage.parsePage(loginLogPage);
    }

    @Override
    public QuestionMapResult findByExamList(int langId, int subject, int category) {
        QueryWrapper<CategorySetting> categorySettingServiceQueryWrapper = new QueryWrapper<>();
        categorySettingServiceQueryWrapper.lambda().eq(CategorySetting::getCategoryId, category).eq(CategorySetting::getSubject, subject);
        List<CategorySetting> list = categorySettingService.list(categorySettingServiceQueryWrapper);

        Iterator<CategorySetting> iterator = list.iterator();
        List<Map<String, Object>> lista = new ArrayList<>();
        while (iterator.hasNext()) {
            CategorySetting next = iterator.next();
            MyLambdaQueryWrapper<QuestionBank> queryWrapper1 = new MyLambdaQueryWrapper<>();
            queryWrapper1.select(QuestionBank::getId, QuestionBank::getAnswer, QuestionBank::getContent, QuestionBank::getDifficulty,
                    QuestionBank::getRemark,
                    QuestionBank::getSort, QuestionBank::getSubject, QuestionBank::getTags, QuestionBank::getType)
                    .select(QuestionBank::getImageId).as("imageId").select(QuestionBank::getLanguageId).as("languageId")
                    .select(QuestionBank::getLearningType).as("learningType")
                    .select(QuestionBank::getRemarkAudioId).as("remarkAudioId")
                    .select(QuestionBank::getImageId).as("imageId")

                    .select(QuestionBank::getOptionA).as("optionA")
                    .select(QuestionBank::getOptionB).as("optionB")
                    .select(QuestionBank::getOptionC).as("optionC")
                    .select(QuestionBank::getOptionD).as("optionD");

                  //  .picture(QuestionBank::getImageId).as("imageUrl");
          //  queryWrapper1.orderBy("RAND()");
            queryWrapper1.eq(QuestionBank::getLanguageId, langId).eq(QuestionBank::getSubject, subject).eq(QuestionBank::getType, next.getType()).inSql(QuestionBank::getId, SqlModel.getCategoryFindId(category)).last("ORDER BY RAND() ASC LIMIT " + next.getCount());
            List<Map<String, Object>> list1 = this.listMaps(queryWrapper1);
            lista.addAll(list1);
        }

        QuestionMapResult questionResult = new QuestionMapResult();
        questionResult.setList(lista);
        questionResult.setTotal(lista.size());


        return questionResult;
    }

    @Cacheable(value = "question", key = "'lang'+#langId+'::path::findByExamAllList::'+'::subject'+#subject+'category'+#category+'::pageSize'+#pageSize+'::current'+#current")
    @Override
    public MyPage findByExamAllList(int pageSize, int current, int langId, int subject, int category) {
        QueryWrapper<QuestionBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("SORT");
        queryWrapper.lambda().eq(QuestionBank::getLanguageId, langId).eq(QuestionBank::getSubject, subject).inSql(QuestionBank::getId, SqlModel.getCategoryFindId(category));
        Page<QuestionBank> page = new Page<>(current, pageSize);
        IPage<QuestionBank> loginLogPage = this.baseMapper.selectPage(page, queryWrapper);
        return MyPage.parsePage(loginLogPage);
    }

    //"'findByLearnList'+#langId+'type'+#type+'subject'+#subject+'category'+#category"
   // @Cacheable(value = "question", key = "'lang'+#langId+'::path::findByLearnList::'+'type'+#type+'::subject'+#subject+'::category'+#category")
    @Override
    public QuestionMapResult findByLearnList(int langId, int type, int subject, int category) {
        long userId = 1L;
        MyLambdaQueryWrapper<QuestionBank> queryWrapper1 = new MyLambdaQueryWrapper<>();
        queryWrapper1.select(QuestionBank::getId, QuestionBank::getAnswer, QuestionBank::getContent, QuestionBank::getDifficulty,
                QuestionBank::getRemark,
                QuestionBank::getSort, QuestionBank::getSubject, QuestionBank::getTags, QuestionBank::getType)
                .select(QuestionBank::getImageId).as("imageId").select(QuestionBank::getLanguageId).as("languageId")
                .select(QuestionBank::getLearningType).as("learningType")
                .select(QuestionBank::getRemarkAudioId).as("remarkAudioId")
              //  .select(QuestionBank::getImageId).as("imageId")

                .select(QuestionBank::getOptionA).as("optionA")
                .select(QuestionBank::getOptionB).as("optionB")
                .select(QuestionBank::getOptionC).as("optionC")
                .select(QuestionBank::getOptionD).as("optionD")

                .picture(QuestionBank::getImageId).as("imageUrl");

        queryWrapper1.eq(QuestionBank::getLanguageId, langId).eq(QuestionBank::getSubject, subject).inSql(QuestionBank::getId, SqlModel.getCategoryFindId(category));
        queryWrapper1.last("ORDER BY RAND(ID)");

        switch (type) {
            case 2:
                break;
            case 3:
                queryWrapper1.isNotNull(QuestionBank::getImageId);
                break;
            case 4:
                queryWrapper1.inSql(QuestionBank::getId, SqlModel.getQuestionErrorsFindId(userId));
                break;
            case 5:
              //  queryWrapper1.notInSql(QuestionBank::getId, SqlModel.getQuestionErrorsFindId(userId));
                break;
            case 6:
                queryWrapper1.inSql(QuestionBank::getId, SqlModel.getQuestionErrorsCountFindId());
                break;
            case 7:

                break;
            case 8:
queryWrapper1.eq(QuestionBank::getType,3);
                break;
            case 9:
                queryWrapper1.eq(QuestionBank::getType,1);
                break;
            case 10:
                queryWrapper1.eq(QuestionBank::getType,2);
                break;
        }


        List<Map<String, Object>> list = this.listMaps(queryWrapper1);
        QuestionMapResult questionResult = new QuestionMapResult();
        questionResult.setList(list);
        questionResult.setTotal(list.size());


        return questionResult;
    }
}




